/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Example_05_Comparable.java]
 * Creation Date        [01-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_06_collections;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * 自定义排序需要实现java.lang.Comparable接口
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_05_Comparable {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 * 
	 */
	public static void main(String[] args) {
		// 按照ID进行排序
		TreeSet<Student> set = new TreeSet<Student>();

		Student sc1 = new Student();
		sc1.setId(1);
		sc1.setAge(10);
		Student sc2 = new Student();
		sc2.setId(2);
		sc2.setAge(40);
		Student sc3 = new Student();
		sc3.setId(3);
		sc3.setAge(30);
		Student sc4 = new Student();
		sc4.setId(4);
		sc4.setAge(50);
		Student sc5 = new Student();
		sc5.setId(5);
		sc5.setAge(20);

		set.add(sc5);
		set.add(sc3);
		set.add(sc1);
		set.add(sc4);
		set.add(sc2);

		System.out.println(set);

		// 按照年龄进行排序, 这里使用了指定的比较器
		TreeSet<Student> set2 = new TreeSet<Student>(new Comparator<Student>() {
			@Override
			public int compare(Student student1, Student student2) {
				return student1.getAge() - student2.getAge();
			}
		});

		set2.addAll(set);
		System.out.println(set2);

	}

}

/**
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class Student implements Comparable<Student> {

	private int id;

	private int age;

	private String name;

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age
	 *            the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 重写hashCode()方法
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	/**
	 * 重写equals()方法
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (id != other.id)
			return false;
		return true;
	}

	/**
	 * 重写toString()方法
	 */
	@Override
	public String toString() {
		return "StudentComparable [id=" + id + "]";
	}

	/**
	 * 重写CompareTo()方法
	 */
	@Override
	public int compareTo(Student student) {
		// JDK7 可用下面的方式比较
		// return Integer.compare(this.id, student.id);
		return id - student.id;
	}
}
